var index = {
    color: ['rgb(68,114,196)', 'rgb(237,125,49)', 'rgb(165,165,165)', 'rgb(255,192,0)', 'rgb(153,216,123)',
        "rgb(0,174,157)", 'rgb(101,194,148)', 'rgb(199,126,181)', 'rgb(0,154,214)', 'rgb(175,180,219)'],
    yearXAxis: ['2019', '2020', '2021'],
    toolBoxShow: false,
    //样本数量及占比
    chart1: function (data) {
        $('.sampleSum').text(data['sampleSum']);
        var vos = data['roleSampleVos'];
        var str = '';
        for (var key in vos) {
            var temp = vos[key];
            for (var t in temp) {
                if (!temp[t]) {
                    temp[t] = 0;
                }
            }
            str += '<tr>\n' +
                '                <td>' + temp['xd'] + '</td>\n' +
                '                <td>' + temp['studentNum'] + '</td>\n' +
                '                <td>' + temp['studentPercent'] + '%</td>\n' +
                '                <td>' + temp['teacherNum'] + '</td>\n' +
                '                <td>' + temp['teacherPercent'] + '%</td>\n' +
                '                <td>' + temp['parentNum'] + '</td>\n' +
                '                <td>' + temp['parentPercent'] + '%</td>\n' +
                '                <td>' + temp['yzNum'] + '</td>\n' +
                '                <td>' + temp['yzPercent'] + '%</td>\n' +
                '                <td>' + temp['rddbNum'] + '</td>\n' +
                '                <td>' + temp['rddbPercent'] + '%</td>\n' +
                '                <td>' + temp['allNum'] + '</td>\n' +
                '            </tr>'
        }
        $(".myTbody").html(str);
    },
    getLegend: function (areaName) {
        if (areaName == '市属' || areaName == '贵安新区') {
            return ['2021'];
        } else {
            return [areaName, '贵阳市'];
        }
    },

    getXAxis: function (areaName) {
        if (areaName == '市属' || areaName == '贵安新区') {
            return [areaName, '贵阳市'];
        } else {
            return index.yearXAxis;
        }
    },
    //获取显示数据
    getShowSeries: function (data, areaName) {
        var series = [];
        if (areaName == '市属' || areaName == '贵安新区') {
            series = [
                {
                    name: '2021',
                    type: 'bar',
                    barGap: 0,
                    data: data['2021'],
                    label: {
                        normal: {
                            show: true,//开启显示
                            position: 'top',//柱形上方
                            textStyle: { //数值样式
                                color: '#02030a'
                            },
                            formatter: function (value) {
                                return value.data + "%"
                            }
                        }
                    }
                }
            ];
        } else {
            series = [{
                name: areaName,
                type: 'bar',
                barGap: 0,
                data: data[areaName],
                label: {
                    normal: {
                        show: true,//开启显示
                        position: 'top',//柱形上方
                        textStyle: { //数值样式
                            color: '#02030a'
                        },
                        formatter: function (value) {
                            return value.data + "%"
                        }
                    }
                }
            }, {
                name: '贵阳市',
                type: 'bar',
                barGap: 0,
                data: data['贵阳市'],
                label: {
                    normal: {
                        show: true,//开启显示
                        position: 'top',//柱形上方
                        textStyle: { //数值样式
                            color: '#02030a'
                        },
                        formatter: function (value) {
                            return value.data + "%"
                        }
                    }
                }
            }];

        }
        return series;

    },
    xAxis2Legend: function (txtId, txt, div, data, areaName, dataDeatil) {
        $("." + txtId).text(data[txt]);//文本赋值
        var myChart = echarts.init(document.getElementById(div));
        var option = {
            animation: false,
            color: index.color,
            tooltip: {
                trigger: 'axis',
                axisPointer: {
                    type: 'shadow'
                }
            },
            legend: {
                data: index.getLegend(areaName),
                top: 'bottom'
            },
            toolbox: {
                show: index.toolBoxShow,
                orient: 'vertical',
                right: 'right',
                feature: {
                    mark: {show: true},
                    dataView: {show: true, readOnly: false},
                    magicType: {show: true, type: ['line', 'bar', 'stack', 'tiled']},
                    restore: {show: true},
                    saveAsImage: {show: true}
                }
            },
            xAxis: [
                {
                    type: 'category',
                    axisTick: {show: false},
                    data: index.getXAxis(areaName)
                }
            ],
            yAxis: [
                {
                    type: 'value'
                }
            ],
            series: index.getShowSeries(data[dataDeatil], areaName)

        };
        // myChart.clear();
        myChart.setOption(option);
    },
    //当区综合满意情况
    chart2: function (data, areaName) {
        index.xAxis2Legend('txt1', 'totalSatisfaction', 'allSatisfaction', data, areaName, 'areaHistoryList');
    },
    //获取
    getSeries: function (data, areaName) {
        var series = [];
        if (areaName == '市属' || areaName == '贵安新区') {
            var serie = {
                name: '2021',
                type: 'bar',
                data: data['2021'],
                label: {
                    normal: {
                        show: true,//开启显示
                        position: 'top',//柱形上方
                        textStyle: { //数值样式
                            color: '#02030a'
                        },
                        formatter: function (value) {
                            return value.data + "%"
                        }
                    }
                }
            };
            series.push(serie);
            return series;
        }
        series = [
            {
                name: '2019',
                type: 'bar',
                barGap: 0,
                data: data['2019'],
                label: {
                    normal: {
                        show: true,//开启显示
                        position: 'top',//柱形上方
                        textStyle: { //数值样式
                            color: '#02030a'
                        },
                        formatter: function (value) {
                            return value.data + "%"
                        }
                    }
                }
            },
            {
                name: '2020',
                type: 'bar',
                data: data['2020'],
                label: {
                    normal: {
                        show: true,//开启显示
                        position: 'inside',//柱形上方
                        textStyle: { //数值样式
                            color: '#02030a'
                        },
                        formatter: function (value) {
                            return value.data + "%"
                        }
                    }
                }
            },
            {
                name: '2021',
                type: 'bar',
                data: data['2021'],
                label: {
                    normal: {
                        show: true,//开启显示
                        position: 'top',//柱形上方
                        textStyle: { //数值样式
                            color: '#02030a'
                        },
                        formatter: function (value) {
                            return value.data + "%"
                        }
                    }
                }
            }
        ];
        return series;
    },
    chart3: function (data, areaName) {
        $(".txt2").text(data['xdTotalSatisfaction']);//文本赋值
        var myChart = echarts.init(document.getElementById('allSatisfactionOnXd'));
        var option = {
            animation: false,
            color: index.color,
            tooltip: {
                trigger: 'axis',
                axisPointer: {
                    type: 'shadow'
                }
            },
            legend: {
                data: ['2019', '2020', '2021'],
                top: 'bottom'
            },
            toolbox: {
                show: index.toolBoxShow,
                orient: 'vertical',
                right: 'right',
                feature: {
                    mark: {show: true},
                    dataView: {show: true, readOnly: false},
                    magicType: {show: true, type: ['line', 'bar', 'stack', 'tiled']},
                    restore: {show: true},
                    saveAsImage: {show: true}
                }
            },
            xAxis: [
                {
                    type: 'category',
                    axisTick: {show: false},
                    data: ['均值', '幼儿园', '小学', '初中', '高中']
                }
            ],
            yAxis: [
                {
                    type: 'value'
                }
            ],
            series: index.getSeries(data['xdTotalSatisfactionRate'], areaName)
        };
        // myChart.clear();
        myChart.setOption(option);
    },

    //区县学生满意度
    chart4: function (data, areaName) {
        index.xAxis2Legend('txt3', 'studentTotalSatisfaction', 'allSatisfactionOnStudent', data, areaName, 'studentTotalSatisfactionRate');
    },
    //区县教师满意度
    chart5: function (data, areaName) {
        index.xAxis2Legend('txt4', 'teacherTotalSatisfaction', 'allSatisfactionOnTeacher', data, areaName, 'teacherTotalSatisfactionRate');
    },
    //区县家长
    chart6: function (data, areaName) {
        index.xAxis2Legend('txt5', 'parentTotalSatisfaction', 'allSatisfactionOnParent', data, areaName, 'parentTotalSatisfactionRate');
    },
    //转换函数特针对特殊角色
    xAxis2LegendForNewRole: function (txtId, txt, div, data, areaName, dataDeatil) {
        $("." + txtId).text(data[txt]);//文本赋值
        var myChart = echarts.init(document.getElementById(div));
        var option = {
            animation: false,
            color: index.color,
            tooltip: {
                trigger: 'axis',
                axisPointer: {
                    type: 'shadow'
                }
            },
            legend: {
                data: ['2021'],
                top: 'bottom'
            },
            toolbox: {
                show: index.toolBoxShow,
                orient: 'vertical',
                right: 'right',
                feature: {
                    mark: {show: true},
                    dataView: {show: true, readOnly: false},
                    magicType: {show: true, type: ['line', 'bar', 'stack', 'tiled']},
                    restore: {show: true},
                    saveAsImage: {show: true}
                }
            },
            xAxis: [
                {
                    type: 'category',
                    axisTick: {show: false},
                    data: [areaName, '贵阳市']
                }
            ],
            yAxis: [
                {
                    type: 'value'
                }
            ],
            series: [
                {
                    name: '2021',
                    type: 'bar',
                    barGap: 0,
                    data: data[dataDeatil]['2021'],
                    label: {
                        normal: {
                            show: true,//开启显示
                            position: 'top',//柱形上方
                            textStyle: { //数值样式
                                color: '#02030a'
                            },
                            formatter: function (value) {
                                return value.data + "%"
                            }
                        }
                    }
                }
            ]
        };
        // myChart.clear();
        myChart.setOption(option);
    },
    //区县幼儿园园长
    chart7: function (data, areaName) {
        index.xAxis2LegendForNewRole("txt6", "yeyYzTotalSatisfaction", 'allSatisfactionOnYz', data, areaName, 'yeyYzTotalSatisfactionRate');
    },
    //区县人大代表/政协委员
    chart8: function (data, areaName) {
        index.xAxis2LegendForNewRole('txt7', 'yeyRddbTotalSatisfaction', 'allSatisfactionOnRddb', data, areaName, 'yeyRddbTotalSatisfactionRate');
    },

    //当区高中综合满意率
    chart9: function (data, areaName) {
        index.xAxis2Legend('txt8', 'gzTotalSatisfaction', 'allSatisfactionOnSenior', data, areaName, 'gzTotalSatisfactionRate');
    },

    //当区高中学生综合满意率
    chart10: function (data, areaName) {
        index.xAxis2Legend('txt9', 'gzStudentTotalSatisfaction', 'allSatisfactionOnSeniorStudent', data, areaName, 'gzStudentTotalSatisfactionRate');
    },

    //当区高中教师综合满意率
    chart11: function (data, areaName) {
        index.xAxis2Legend('txt10', 'gzTeacherTotalSatisfaction', 'allSatisfactionOnSeniorTeacher', data, areaName, 'gzTeacherTotalSatisfactionRate');
    },

    //当区高中家长综合满意率
    chart12: function (data, areaName) {
        index.xAxis2Legend('txt11', 'gzParentTotalSatisfaction', 'allSatisfactionOnSeniorParent', data, areaName, 'gzParentTotalSatisfactionRate');
    },

    //处理分项目数据
    itemDataDeal: function (itemObject) {
        var arr = [];
        for (var obj in itemObject) {
            if (obj !== 'indexName') {
                var serie = {
                    name: obj,
                    type: 'bar',
                    data: itemObject[obj],
                    label: {
                        normal: {
                            show: true,//开启显示
                            position: 'top',//柱形上方
                            textStyle: { //数值样式
                                color: '#02030a'
                            }
                        }
                    }
                    // ,
                    // stack: "1"
                };
                arr.push(serie);
            }

        }
        return arr;
    },

    getItemLegend: function (data) {
        return Object.getOwnPropertyNames(data).slice(1, 3);
    },
    // 分项目显示情况
    itemDataShow: function (txtId, txt, div, data, dataDetail) {
        $("." + txtId).text(data[txt]);//文本赋值
        var myChart = echarts.init(document.getElementById(div));
        var option = {
            animation: false,
            color: index.color,
            tooltip: {
                trigger: 'axis',
                axisPointer: {
                    type: 'shadow'
                }
            },
            legend: {
                data: index.getItemLegend(data[dataDetail]),
                top: 'bottom'
            },
            toolbox: {
                show: index.toolBoxShow,
                orient: 'vertical',
                right: 'right',
                feature: {
                    mark: {show: true},
                    dataView: {show: true, readOnly: false},
                    magicType: {show: true, type: ['line', 'bar', 'stack', 'tiled']},
                    restore: {show: true},
                    saveAsImage: {show: true}
                }
            },
            xAxis: [
                {
                    type: 'category',
                    axisTick: {show: false},
                    data: data[dataDetail]['indexName'],
                    axisLabel: {
                        interval: 0
                    }
                }
            ],

            yAxis: [
                {
                    type: 'value'
                }
            ],
            series: index.itemDataDeal(data[dataDetail])
        };
        myChart.clear();
        myChart.setOption(option);
    },

    //当区高中学生分项目满意情况
    chart13: function (data) {
        index.itemDataShow('txt12', 'gzStudentProjectIndicators', 'itemSatisfactionOnSeniorStudent', data, 'gzStudentProjectIndicatorsList');
    },
    //当区高中教师分项目满意情况
    chart14: function (data) {
        index.itemDataShow('txt13', 'gzTeacherProjectIndicators', 'itemSatisfactionOnSeniorTeacher', data, 'gzTeacherProjectIndicatorsList');
    },
    //当区高中家长分项目满意情况
    chart15: function (data) {
        index.itemDataShow('txt14', 'gzParentProjectIndicators', 'itemSatisfactionOnSeniorParent', data, 'gzParentProjectIndicatorsList');
    },


    //当区初中综合满意率
    chart16: function (data, areaName) {
        index.xAxis2Legend('txt15', 'czTotalSatisfaction', 'allSatisfactionOnJunior', data, areaName, 'czTotalSatisfactionRate');
    },

    //当区初中学生综合满意率
    chart17: function (data, areaName) {
        index.xAxis2Legend('txt16', 'czStudentTotalSatisfaction', 'allSatisfactionOnJuniorStudent', data, areaName, 'czStudentTotalSatisfactionRate');
    },

    //当区初中教师综合满意率
    chart18: function (data, areaName) {
        index.xAxis2Legend('txt17', 'czTeacherTotalSatisfaction', 'allSatisfactionOnJuniorTeacher', data, areaName, 'czTeacherTotalSatisfactionRate');
    },

    //当区初中家长综合满意率
    chart19: function (data, areaName) {
        index.xAxis2Legend('txt18', 'czParentTotalSatisfaction', 'allSatisfactionOnJuniorParent', data, areaName, 'czParentTotalSatisfactionRate');
    },

    //当区初中学生分项目满意情况
    chart20: function (data) {
        index.itemDataShow('txt19', 'czStudentProjectIndicators', 'itemSatisfactionOnJuniorStudent', data, 'czStudentProjectIndicatorsList');
    },
    //当区初中教师分项目满意情况
    chart21: function (data) {
        index.itemDataShow('txt20', 'czTeacherProjectIndicators', 'itemSatisfactionOnJuniorTeacher', data, 'czTeacherProjectIndicatorsList');
    },
    //当区初中家长分项目满意情况
    chart22: function (data) {
        index.itemDataShow('txt21', 'czParentProjectIndicators', 'itemSatisfactionOnJuniorParent', data, 'czParentProjectIndicatorsList');
    },

    //当区小学综合满意率
    chart23: function (data, areaName) {
        index.xAxis2Legend('txt22', 'xxTotalSatisfaction', 'allSatisfactionOnPrimary', data, areaName, 'xxTotalSatisfactionRate');
    },
    //当区小学学生综合满意率
    chart24: function (data, areaName) {
        index.xAxis2Legend('txt23', 'xxStudentTotalSatisfaction', 'allSatisfactionOnPrimaryStudent', data, areaName, 'xxStudentTotalSatisfactionRate');
    },

    //当区小学教师综合满意率
    chart25: function (data, areaName) {
        index.xAxis2Legend('txt24', 'xxTeacherTotalSatisfaction', 'allSatisfactionOnPrimaryTeacher', data, areaName, 'xxTeacherTotalSatisfactionRate');
    },

    //当区小学家长综合满意率
    chart26: function (data, areaName) {
        index.xAxis2Legend('txt25', 'xxParentTotalSatisfaction', 'allSatisfactionOnPrimaryParent', data, areaName, 'xxParentTotalSatisfactionRate');
    },

    //当区小学学生分项目满意情况
    chart27: function (data) {
        index.itemDataShow('txt26', 'xxStudentProjectIndicators', 'itemSatisfactionOnPrimaryStudent', data, 'xxStudentProjectIndicatorsList');
    },
    //当区小学教师分项目满意情况
    chart28: function (data) {
        index.itemDataShow('txt27', 'xxTeacherProjectIndicators', 'itemSatisfactionOnPrimaryTeacher', data, 'xxTeacherProjectIndicatorsList');
    },
    //当区小学家长分项目满意情况
    chart29: function (data) {
        index.itemDataShow('txt28', 'xxParentProjectIndicators', 'itemSatisfactionOnPrimaryParent', data, 'xxParentProjectIndicatorsList');
    },


    //当区幼儿园综合满意率
    chart30: function (data, areaName) {
        index.xAxis2Legend('txt29', 'yeyTotalSatisfaction', 'allSatisfactionOnYey', data, areaName, 'yeyTotalSatisfactionRate');
    },

    //当区幼儿园教师综合满意率
    chart31: function (data, areaName) {
        index.xAxis2Legend('txt30', 'yeyTeacherTotalSatisfaction', 'allSatisfactionOnYeyTeacher', data, areaName, 'yeyTeacherTotalSatisfactionRate');
    },

    //当区幼儿园家长综合满意率
    chart32: function (data, areaName) {
        index.xAxis2Legend('txt31', 'yeyParentTotalSatisfaction', 'allSatisfactionOnYeyParent', data, areaName, 'yeyParentTotalSatisfactionRate');
    },

    //当区幼儿园园长综合满意率
    chart33: function (data, areaName) {
        index.xAxis2LegendForNewRole('txt32', 'yeyYzTotalSatisfaction', 'allSatisfactionOnYeyYz', data, areaName, 'yeyYzTotalSatisfactionRate');
    },

    //当区幼儿园人大代表/政协委员综合满意率
    chart34: function (data, areaName) {
        index.xAxis2LegendForNewRole('txt33', 'yeyRddbTotalSatisfaction', 'allSatisfactionOnYeyRddb', data, areaName, 'yeyRddbTotalSatisfactionRate');
    },

    //当区幼儿园教师分项目满意情况
    chart35: function (data) {
        index.itemDataShow('txt34', 'yeyTeacherProjectIndicators', 'itemSatisfactionOnYeyTeacher', data, 'yeyTeacherProjectIndicatorsList');
    },
    //当区幼儿园家长分项目满意情况
    chart36: function (data) {
        index.itemDataShow('txt35', 'yeyParentProjectIndicators', 'itemSatisfactionOnYeyParent', data, 'yeyParentProjectIndicatorsList');
    },
    //当区幼儿园园长分项目满意情况
    chart37: function (data) {
        index.itemDataShow('txt36', 'yeyYzProjectIndicators', 'itemSatisfactionOnYeyYz', data, 'yeyYzProjectIndicatorsList');
    },
    //当区幼儿园人大代表/政协委员分项目满意情况
    chart38: function (data) {
        index.itemDataShow('txt37', 'yeyRddbProjectIndicators', 'itemSatisfactionOnYeyRddb', data, 'yeyRddbProjectIndicatorsList');
    },
    canvas2Img: function (claObject) {
        //将第一个画布作为基准。
        var baseCanvas = $(claObject).find("canvas").first()[0];
        if (!baseCanvas) {
            return false;
        }
        var width = baseCanvas.width;
        var height = baseCanvas.height;
        var ctx = baseCanvas.getContext("2d");
        //遍历，将后续的画布添加到在第一个上
        $(claObject).find("canvas").each(function (i, canvasObj) {
            if (i > 0) {
                var canvasTmp = $(canvasObj)[0];
                ctx.drawImage(canvasTmp, 0, 0, width, height);
            }
        });
        //获取base64位的url
        var base64Str = baseCanvas.toDataURL();
        $(claObject).html("<img src=" + base64Str + " alt='图片不存在'> ");
        return baseCanvas.toDataURL();
    },
    init2: function () {
        var echarts = $('.chart');
        for (var i = 0; i < echarts.length; i++) {
            index.canvas2Img(echarts[i]);
        }
    },
    //初始化
    init: function (areaName, sysCode) {
        layui.use(['layer'], function () {
            var layer = layui.layer;
            var loading = layer.load(1);
            //临时处理
            $.get("/static/json/county/county_report_" + areaName + ".json", {}, function (data) {
            // $.get("/static/json/killInvalidData/county/county_report_" + areaName + ".json", {}, function (data) {
            //临时处理
            // $.get("/reportDataCounty/getData", {areaName: areaName, sysCode: sysCode}, function (data) {
                index.chart1(data);
                index.chart2(data, areaName);
                index.chart3(data, areaName);
                index.chart4(data, areaName);
                index.chart5(data, areaName);
                index.chart6(data, areaName);
                index.chart7(data, areaName);
                index.chart8(data, areaName);
                //高中
                index.chart9(data, areaName);
                index.chart10(data, areaName);
                index.chart11(data, areaName);
                index.chart12(data, areaName);
                index.chart13(data);
                index.chart14(data);
                index.chart15(data);

                //初中
                index.chart16(data, areaName);
                index.chart17(data, areaName);
                index.chart18(data, areaName);
                index.chart19(data, areaName);
                index.chart20(data);
                index.chart21(data);
                index.chart22(data);

                //小学
                index.chart23(data, areaName);
                index.chart24(data, areaName);
                index.chart25(data, areaName);
                index.chart26(data, areaName);
                index.chart27(data);
                index.chart28(data);
                index.chart29(data);

                //幼儿园
                index.chart30(data, areaName);
                index.chart31(data, areaName);
                index.chart32(data, areaName);
                index.chart33(data, areaName);//幼儿园园长
                index.chart34(data, areaName);//人大代表/政协委员
                index.chart35(data);
                index.chart36(data);
                index.chart37(data);//幼儿园园长
                index.chart38(data);//人大代表/政协委员
                index.init2();
                layer.close(loading);
            });

        });


    }
};

